
import { getToken, setToken, removeToken } from '@/utils/auth'

const state = {
  token: getToken(),
  info: {},
  avatar: '',
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_INFO: (state, info) => {
    state.info = info
  },
}

const actions = {
  // user login
  do_set_token({ commit }, data) {
    commit('SET_TOKEN', data.token)
    commit('SET_INFO', data.info)
    setToken(data.token)
  },

  // remove token
  delete_token({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_INFO', {})
      removeToken()
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
